package com.etop.learnapp.adapter;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.etop.learnapp.R;
import com.etop.learnapp.activity.ApplyActivity;
import com.etop.learnapp.fragment.Student_Info_Fragment;
import com.etop.learnapp.personalClass.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Arron on 2015/11/6.
 */
public class Student_Adapter extends BaseAdapter {

    private Context context;
    public Button deleteBtn, btnEditStudent;
    private ArrayList<Student> studentArrayList;

    private List list;

    public Student_Adapter(Context context, List list) {
        this.context = context;
        this.list = list;
        studentArrayList = ((ApplyActivity) context).studentArrayList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        convertView = LayoutInflater.from(context).inflate(R.layout.student_line, null);

        deleteBtn = (Button) convertView.findViewById(R.id.btn_Student_delete);
        btnEditStudent = (Button) convertView.findViewById(R.id.btn_edit_student);

        if (studentArrayList.get(position) == null) ((ApplyActivity) context).ExtendStudentList();
        if (studentArrayList.get(position).getName() != null)
            btnEditStudent.setText(studentArrayList.get(position).getName().toString());

        btnEditStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student_Info_Fragment student_info_fragment = new Student_Info_Fragment();
                FragmentManager fragmentManager = ((ApplyActivity) context).getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_Main, student_info_fragment);
                fragmentTransaction.addToBackStack(null);//加入模拟栈
                fragmentTransaction.commit();

                student_info_fragment.setPosition(position);
            }
        });

        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                studentArrayList.remove(position);
                list.remove(position);
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

}